<?php

/* 
 * 代码Controller
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
class codeController extends AppController
{
    public function __construct() {
        parent::__construct();
        $this->modelObj = new CodeModel();
        $this->log_type = '代码管理';
        $this->cookie_key = 'code_cookie';
    }
    /**
     * 浏览
     */
    public function view(){
        $this->_get_data();
        $this->display('code_list');
    }
    public function query(){
        $this->_get_data();        
        $this->ret['status'] = 0;
        $this->ret['content'] = $this->fetch('code_list');
        exit(json_encode($this->ret));
    }   
    /**
     * 生成
     */
    public function create(){
        $this->title = '添加代码';
        $this->create_token();
        $this->get_cookie();
        $appkey = ApiBase::load()->get_unique_id(32, 'APP');        
        $user_list = AdminUserModel::load()->get_list();        
        $this->assign('appid', $this->_create_appid());
        $this->assign('appkey', $appkey);
        $this->assign('user_list', $user_list);        
        $this->display('code_create');
    }
    /**
     * 写入数据库
     */
    public function new_create(){
        $this->check_token();        
        $data = array(
            'code_name'     => trim($_POST['code_name']),
            'user_id'       => intval($_POST['user_id']),
            'appid'         => trim($_POST['appid']),
            'appkey'        => trim($_POST['appkey']),
            'code_url'      => addslashes($_POST['code_url']),
            'in_time'       => time(),
        );
        $this->set_cookie($data);
        if($_POST['code_name'] > 0) {
            $this->show_msg('请填寫名称');
        }
        if($_POST['user_id'] <= 0) {
            $this->show_msg('請選擇用戶名稱');
        }
        if(empty($_POST['appid'])) {            
            $this->show_msg('請生成appid');
        } 
        if(empty($_POST['appkey'])) {
            $this->show_msg('請生成appkey');
        }
        if(empty($_POST['code_url'])) {
            $this->show_msg('请生成URL');
        }
        //检查代码名称是否重复
        $user_bool = CodeModel::load()->check_code_user($_POST['code_name']);
        if($user_bool) {
            $this->show_msg('代碼名稱重復');
        }
        //判断appid 与 url里的appid是否一致
        $url_appid = p_getUrlParams($_POST['code_url'], 'appid');
        if($_POST['appid'] != $url_appid) {
            $this->show_msg('产生的APPID和URL参数里APPID不一致，请重新生成');
        }
        //检查代码APPID是否重复
        $appid_bool = CodeModel::load()->check_appid($_POST['appid']);
        if($appid_bool) {
            $this->show_msg('APPID重复，请刷新页面');
        }
        $bool = CodeModel::load()->in_data($data);
        if($bool) {
            $this->admin_log('添加代码:' . $data['code_name']);
            $this->show_msg('添加成功', 'c=code&m=view');
        } else {
            $this->show_msg('添加失败', 'c=code&m=create');
        }
    }

    /**
     * 获取数据
     */
    private function _get_data(){
        $page = isset($_REQUEST['page']) ? $_REQUEST['page'] : 1;
        $where = '';
        $filter['user_name'] = isset($_POST['user_name']) ? $_POST['user_name'] : '';
        $filter['appid'] = isset($_POST['appid']) ? $_POST['appid'] : '';
        $filter['appkey'] = isset($_POST['appkey']) ? $_POST['appkey'] : '';
        $filter['start_time'] = isset($_POST['start_time']) ? $_POST['start_time'] : '';
        $filter['end_time'] = isset($_POST['end_time']) ? $_POST['end_time'] : '';
        if(!empty($filter['user_name'])) {
            $user_id = AdminUserModel::load()->get_user_id($filter['user_name']);
            $where .= ' and user_id = ' . $user_id;
        }
        if(!empty($filter['appid'])) {
            $where .= ' and instr(appid, ' . "'{$filter['appid']}')";
        }
        if(!empty($filter['appkey'])) {
            $where .= ' and instr(appkey, ' . "'{$filter['appkey']}')";
        }
        if(!empty($filter['start_time'])) {
            $start_time = publicController::load()->strToTime($filter['start_time']);
            $where .= ' and in_time < ' . $start_time;
        }
        if(!empty($filter['end_time'])) {
            $end_time = publicController::load()->strToTime($filter['start_time']);
            $where .= ' and in_time > ' . $end_time;
        }
        $sort_by = isset($_REQUEST['sort_by']) ? $_REQUEST['sort_by'] : $this->modelObj->keyFields;
        $sort_order = isset($_REQUEST['sort_order']) ? $_REQUEST['sort_order'] : 'DESC';
        $pageObj = new pageModel('code', $where, $this->page_number, 10, $sort_by, $sort_order);        
        $data = $pageObj->pageData($page);    
        $list = $data['pageData'];
        if($list) {
            foreach ($list as $key => $row) {  
                $user_id = $row['user_id'];
                $user_name = AdminUserModel::load()->get_user_name($user_id);
                $data['pageData'][$key]['id'] = $key + 1;
                $data['pageData'][$key]['user_name'] = $user_name;
                $data['pageData'][$key]['in_time_format'] = date("Y/m/d H:i:s", $row['in_time']);
                $data['pageData'][$key]['last_time_format'] = empty($row['last_time']) ? '' : publicController::load()->friendlyDate($row['last_time']);
                $data['pageData'][$key]['is_use'] = $row['is_use'] == 1 ? 'glyphicon-ok' : 'glyphicon-remove';
            }
        }
        $this->assign('sort_' . $sort_by, $data['sort_img']);
        $this->assign('data', $data);
    }
    /**
     * 生成唯一的appid
     * @return type
     */
    private function _create_appid() {
        while(true) {
            $appid = ApiBase::load()->getNonceStr(4);
            $appid_bool = CodeModel::load()->check_appid($appid);
            if(!$appid_bool) {
                return $appid;
            }
        }
    }
}

